﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Co9320.NamaAlert.Model;
using Co9320.NamaAlert.Extension.Model;
using System.Security;

namespace Co9320.NamaAlert.Extension
{
    [Plugin("8928CA59-DDC2-4288-9CE1-2038E496577C", HasEditableOptions = true, RequireLiveProgramNotify = LiveProgramNotify.FavoriteOnly, PublisherUrl = "http://twitter.com/")]
    public class TwitterNotify : IPlugin
    {
        private Setting settings;
        private Twitter twitter;

        public TwitterNotify()
        {
        }

        public string UserName
        {
            get { return settings.UserName; }
            set
            {
                settings.UserName = value;
            }
        }

        public SecureString Password
        {
            get { return settings.Password; }
            set
            {
                settings.Password = value;
            }
        }

        public void Initialize(IPluginSettings settings)
        {
            this.settings = new Setting(settings);
            this.twitter = new Twitter(this.settings);
        }

        public void EditOptions(System.Windows.Window dialogOwner)
        {
            OptionWindow window = new OptionWindow();
            OptionViewModel context = new OptionViewModel(setting);
            window.DataContext = context;

            if (window.ShowDialog() == true)
            {
                OnRequestSaveSettings(this, new SaveSettingsEventArgs(setting.PluginSettings));
            }
            return;
        }

        public void ExecuteAlert(LiveProgram program)
        {
            twitter.SendUpdateAsync("また見てる: " + program.Title + " " + program.Uri.ToString());
        }

        public void ExecuteAction(LiveProgram program)
        {
            twitter.SendUpdateAsync(":" + program.Title + " " + program.Uri.ToString());
        }

        public void ExecuteWhenLiveProgramStarted(LiveProgram liveProgram)
        {
            twitter.SendUpdateAsync("放送開始:" + liveProgram.Title + " " + liveProgram.Uri.ToString());
        }

        #region イベント

        public event EventHandler<OpenUriEventArgs> RequestOpenUri;

        public event EventHandler<SaveSettingsEventArgs> RequestSaveSettings;

        public event EventHandler<ShowAlertWindowEventArgs> RequestShowAlertWindow;

        public event EventHandler<ShowBalloonTipEventArgs> RequestShowBalloonTip;

        public event EventHandler<SystemMessageEventArgs> SendSystemMessage;

        private void OnRequestSaveSettings(object sender, SaveSettingsEventArgs e)
        {
            if (RequestSaveSettings != null)
            {
                RequestSaveSettings(sender, e);
            }
        }
        #endregion
    }
}
